package com.starbaba.userpoint.view;

import com.base.common.database.BaseDao;
import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.userpoint.model.UserPointModel;
import com.starbaba.userpoint.service.UserPointService;
import com.starbaba.userpoint.service.impl.UserPointServiceImpl;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

@SuppressWarnings({ "serial", "unchecked", "rawtypes", "unused" })
public class UserPointAction extends BaseAction implements ModelDriven<UserPointModel> {

	@SuppressWarnings("deprecation")
	final BaseDao dao = new BaseDao(Constant.POOL_LIFE);

	private String fileName;
	private InputStream input;

	public String getFileName() {
		return fileName;
	}

	public InputStream getInput() {
		return input;
	}

	private UserPointModel model;
	private UserPointService service = new UserPointServiceImpl();

	@Override
	public UserPointModel getModel() {
		if (model == null) {
			model = new UserPointModel();
		}
		return model;
	}

	public String show() {
		return SUCCESS;
	}
    
	/**
	 * 用户积分明细列表
	 */
	public void queryDataList() {

		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();

		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		List<UserPointModel> list = service.getDataList(start, length, pageResponse, paramsMap);
		//对mobnum中间4位转为*
		for (int i = 0; i <list.size() ; i++) {
			UserPointModel userPointModel = list.get(i);
			userPointModel.setMobnum(PhoneNumNideUtil.hideNum(userPointModel.getMobnum()));
			list.set(i,userPointModel);
		}
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    
	/**
	 * 用户积分列表
	 */
	public void queryUserPointList() {

		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();

		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		List<UserPointModel> list = service.getUserPointList(start, length, pageResponse, paramsMap);
		//对用户的手机号中间四位进行转为*
		for (int i = 0; i <list.size() ; i++) {
			UserPointModel userPointModel = list.get(i);
			userPointModel.setMobnum(PhoneNumNideUtil.hideNum(userPointModel.getMobnum()));
			list.set(i,userPointModel);
		}
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 用户积分管理
	 */
	public void userPointManage() {

		ResultJson resultJs = new ResultJson();
		JsonObject result = new JsonObject();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();

		try {
			resultJs.msg = service.userPointManage(paramsMap);
			result.add("result", resultJs.toJSON());
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String exportDetailCVS() throws UnsupportedEncodingException {

		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);

		pageRequest.setIDisplayStart(-1);
		pageRequest.setIDisplayLength(-1);
		pageRequest.setOrderby("createtime");
		pageRequest.setOrder("desc");

		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		List<UserPointModel> list = service.getDataList(start, length, pageResponse, paramsMap);
		String value = service.exportLoanOrderCVS(list);
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}
}
